#ChangChun 2 KFM parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class ChangChun2_kmf(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(ChangChun2_kmf,self).__init__("KMF")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        self.modelType = 0
        
    #TO DO
    def read_name(self, pad):
        '''Read bytes until null byte, then skip the rest of the section'''
        
        name = ""
        char = self.inFile.read_char()
        while char != "\x00":
            name = ''.join([name, char])
            char = self.inFile.read_char()
        self.inFile.seek(pad - len(name) - 1, 1)        
        return name
    
    #TO DO
    def parse_bones(self, numBones):
        
        for i in range(numBones):
            self.inFile.seek(172, 1)
    
    #TO DO
    def parse_vertices(self, meshName, numVerts):
        
        for i in range(numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            self.inFile.read_float(4)
            
            nx, ny, nz = self.inFile.read_float(3)
            tu, tv = self.inFile.read_float(2)
            self.inFile.read_float(6)
            
            
            vx = float(vx) * -1
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_uv(meshName, i, [tu, tv])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
    
    def parse_faces(self, meshName, numFaces, meshNum):
        
        for i in range(numFaces):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, meshNum)
            
    def parse_materials(self, numMesh):
        
        for i in range(numMesh):
            texName = self.read_name(44)
            matName = texName[:-4]
            self.create_material(i)
            self.add_material_name(i, matName)
            self.add_texture_name(i, texName)
        
    def parse_file(self):
        '''Main parser method. Can be replaced'''
        
        magic = self.inFile.read_long()
        unk1 = self.inFile.read_long()
        unk2 = self.inFile.read_long() #always E8 03 00 00
        numBones = self.inFile.read_long()
        unk4 = self.inFile.read_long() #bone related?
        
        numMesh, totalVerts, totalFaces = self.inFile.read_long(3)
        unk6 = self.inFile.read_long() #?
        self.inFile.read_float(12)
        modelName = self.read_name(28)
        
        unk8 = self.inFile.read_float()
        null1 = self.inFile.read_long()
        unk9 = self.inFile.read_long() #??
        
        self.parse_bones(numBones)
        self.parse_materials(numMesh)

        for i in range(numMesh):
            meshName = "%s[%d]" %(modelName, i)
            self.create_object(meshName)
            numVerts = self.inFile.read_long()
            numFaces  = self.inFile.read_long() / 3
            self.parse_vertices(meshName, numVerts)
            self.parse_faces(meshName, numFaces, i)
        
        null = self.inFile.seek(8, 1)
        
def read_file(path):
    '''Read the file'''
    
    openFile = StructReader(open(path, 'rb'))
    obj = ChangChun2_kmf(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def write_file(path):
    
    pass

def definitions():
    '''Return the header, extension, and a description of the format'''
    
    return "\x11\x27\x00\x00", "KFM", "ChangChun2 KFM"

if __name__ == '__main__':
    
    file1 = "c_item_fishing_rod_01.KFM"
    file2 = "npc_domain_stoneB_02.KFM"
    file3 = "itemW_BOW_01.KFM"
    file4 = "mob_golem_01.KFM" #type 3
    file5 = "C_FlameWolf.KFM" #type 1
    file6 = "Collect_mineral_copper1.KFM" # 2 mesh
    file7 = "mob_hwang_sorcerer_01.KFM"
    obj = read_file(file2)